package com.yanyan.sannuomin.controller;


import com.fasterxml.jackson.databind.ObjectMapper;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanyan.sannuomin.domain.SccDrugstore;
import com.yanyan.sannuomin.domain.SccUserLogin;
import com.yanyan.sannuomin.result.Result;
import com.yanyan.sannuomin.result.ResultCode;
import com.yanyan.sannuomin.service.AccountService;


import com.yanyan.sannuomin.util.MD5;
import com.yanyan.sannuomin.vo.AccountVo;

import com.yanyan.sannuomin.vo.AccountVo1;
import com.yanyan.sannuomin.vo.Repassword;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * Account 接口
 */
@CrossOrigin
@RestController
@RequestMapping("/account")
@Api(tags = "用户管理接口")
public class AccountController {

    @Resource
    private AccountService accountService;



    /**
     * 查询用户
     * @param
     * @param
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "findAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询用户", notes = "根据条件分页查询", httpMethod = "GET")
    public Result findAccount(Integer page, Integer limit, String searchParams) throws  Exception{
//        System.out.println("我来过了");
//        System.out.println("搜索条件："+searchParams);
        ObjectMapper objectMapper = new ObjectMapper();
        AccountVo1 accountVo1 = null;
        if(searchParams != null){
            accountVo1= objectMapper.readValue(searchParams, AccountVo1.class);
        }
        System.out.println("搜索结果："+accountVo1);
        Page page1 = PageHelper.startPage(page, limit);
        List<AccountVo> accountVos = accountService.findAccount(accountVo1);

        for (AccountVo accountVo : accountVos) {
            accountVo.setFendian(accountVo.getProvince()+" "+accountVo.getCity()+" "+accountVo.getCounty());
        }

        PageInfo pageInfo=new PageInfo(accountVos);
        return Result.ok(ResultCode.SUCCESS,pageInfo.getTotal(),accountVos);

    }




    @RequestMapping(value = "{id}", method = RequestMethod.POST)
    @ApiOperation(value = "删除用户", notes = "删除用户", httpMethod = "POST")
    public Result deleteUsers(@PathVariable("id") Integer id) throws Exception{
        int i = accountService.delete(id);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS, i);
        }else{
            return Result.ok(ResultCode.FAIL, null);
        }

    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "修改用户", notes = "修改用户", httpMethod = "POST")
    public Result updateUsers(AccountVo accountVo) throws Exception{
        int update = accountService.update(accountVo);
        return Result.ok(ResultCode.SUCCESS,update);
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ApiOperation(value = "添加用户", notes = "添加用户", httpMethod = "POST")
    public Result addUsers(SccUserLogin sccUserLogin,Integer storeId,Integer roleId)  throws Exception{
        sccUserLogin.setIsAdmin(4);
        String[] getsalt = MD5.getsalt("123");
        sccUserLogin.setPassword(getsalt[0]);
        sccUserLogin.setCustomerCode("4857");
        sccUserLogin.setToken("4");
        sccUserLogin.setPositionId(4);
        int i = accountService.add(sccUserLogin);
        accountService.add1(storeId,sccUserLogin.getId());
        accountService.add2(roleId,sccUserLogin.getId());
        if(i>0){
            return Result.ok(ResultCode.SUCCESS, i);
        }else{
            return Result.ok(ResultCode.FAIL, null);
        }
    }

    @RequestMapping(value = "remover", method = RequestMethod.GET)
    @ApiOperation(value = "重置密码", notes = "重置密码", httpMethod = "GET")
    public Result remover(Repassword repassword) throws Exception{
        String[] getsalt = MD5.getsalt(repassword.getNewpass());
        repassword.setNewpass(getsalt[0]);
        int i = accountService.remover(repassword);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS, i);
        }else{
            return Result.ok(ResultCode.FAIL, null);
        }

    }

//    @RequestMapping(value = "findByAll", method = RequestMethod.POST)
//    @ApiOperation(value = "查询系统用户", notes = "根据条件查询", httpMethod = "POST")
//    public Result findAccountBy(AccountVo accountVo){
//        QueryWrapper<SccRole> queryWrapper=new QueryWrapper<>();
//        queryWrapper.like("name",accountVo.getName());
//        List<SccRole> list = sccServiceImpl.list(queryWrapper);
//
//        return Result.build(list,ResultCodeEnum.SUCCESS);
//    }
//
//    /**
//     * 根据id删除用户
//     * @param roleId
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping(value = "delById", method = RequestMethod.POST)
//    @ApiOperation(value = "根据ID删除用户", notes = "根据ID删除用户", httpMethod = "POST")
//    public Result deleteUserById(int roleId){
//        SccRole sccRole=new SccRole();
//        sccRole.setStatus(0);
//        UpdateWrapper<SccRole> updateWrapper=new UpdateWrapper<>();
//        updateWrapper.eq("role_id",roleId);
//       Boolean isOk=sccServiceImpl.update(sccRole,updateWrapper);
//        return Result.build(isOk,ResultCodeEnum.SUCCESS);
//
//    }

//
//    /**
//     * 批量删除
//     * @param ids
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping(value = "batch", method = RequestMethod.POST)
//    @ApiOperation(value = "批量删除", notes = "批量删除", httpMethod = "POST")
//    public Result batchDeleteUsers(String ids){
//
//        String newIds=ids.substring(0,ids.lastIndexOf(","));
//        String idsArr[] = newIds.split(",");
//        SccRole sccRole=new SccRole();
//        sccRole.setStatus(0);
//
//        UpdateWrapper<SccRole> updateWrapper=new UpdateWrapper<>();
//        updateWrapper.in("role_id",idsArr);
//        Boolean isOk=sccServiceImpl.update(sccRole,updateWrapper);
//        return Result.build(isOk,ResultCodeEnum.SUCCESS);
//    }
//
//    /**
//     * 添加用户
//     * @param
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping(value = "add", method = RequestMethod.POST)
//    @ApiOperation(value = "添加系统用户", notes = "添加", httpMethod = "POST")
//    public Result addUsers(SccRole sccRole){
//        System.out.println(sccRole);
//        Boolean isOk=sccServiceImpl.save(sccRole);
//        return Result.build(isOk,ResultCodeEnum.SUCCESS);
//    }
//
//    @RequestMapping(value = "update", method = RequestMethod.POST)
//    @ApiOperation(value = "修改系统用户", notes = "修改", httpMethod = "POST")
//    public Result updateUsersSccRole (SccRole sccRole){
//        Boolean isOk=sccServiceImpl.updateById(sccRole);
//        return Result.build(isOk,ResultCodeEnum.SUCCESS);
//    }
//

}
